<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo    |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\CarResource;

/**
 * 车辆
 * @author       Eric Lee
 */
class Car {

    private $car_id = null;

    /**
     * 构造
     * @param    int $car_id 车辆id
     * @return   void
     */
    public function __construct($car_id) {
        $this->car_id = $car_id;
    }

    /**
     * 车辆基本信息
     * @return array
     * */
    public function detail($table = 'cars') {
        return M($table)->where("car_id='" . $this->car_id . "'")->find();
    }

    /**
     * 车辆基本信息
     * @return array
     * */
    public function get_dm_pos() {
        return M('car_damage')->alias('cd')->join(C('DB_PREFIX') . 'car_damage_pics cdp ON cd.dm_id=cdp.dm_id')->where("cd.car_id='".$this->car_id . "'")->select();
    }
    /**
     * 获取车辆拍摄位置图片
     * @param int $car_id
     * @param string $pic_key
     * @return mixed
     */
    public static function get_pic_by_key($car_id, $pic_key) {
        $map = array();
        $map['car_id'] = $car_id;
        $map['pic_key'] = $pic_key;
        $arr = M('car_pictures')->where($map)->find();
        $arr['pic_url'] = get_content_cdnurl().$arr['pic_url'];
        return $arr;
    }

    /**
     * 获取车辆图片信息
     * @param string $cond 条件
     * @return array
     * */
    public function get_pics($table = 'car_pictures', $cond = '') {
        return M($table)->where($cond)->select();
    }

    /**
     * 更新车源
     * @param $data array 更新数据
     * @return Boolean
     * */
    public function update($data, $table = 'cars') {
        return M($table)->where("car_id='" . $this->car_id . "'")->save($data);
    }

    /**
    * 评估报告
    * @return   array
    */
    public function report() {
        $data = array();

        //车辆基本信息
        $data['car_base'] = M('cars')->find($this->car_id);
        //车辆手续信息
        $data['procedure'] = M('car_procedure')->where("car_id='" . $this->car_id . "'")->find();
        //车辆配置信息
        $data['config'] = M('car_config')->where("car_id='" . $this->car_id . "'")->find();
        //车辆基本图片
        $data['pictures'] = M('car_pictures')->where("car_id='" . $this->car_id . "'")->select();
        //车辆修复历史
        $data['repair_history'] = M('car_repair_history')->where("car_id='" . $this->car_id . "'")->find();
        //车辆损伤信息
        $data['damage'] = M('car_damage')->where("car_id='" . $this->car_id . "'")->select();
        foreach($data['damage'] as $k => $v){
            $data['damage'][$k]['describe'] = '';
        }
        //车辆损伤图片信息
        foreach ($data['damage'] as $key => $value) {
            $data['damage'][$key]['urls'] = M('car_damage_pics')->where("dm_id='" . $value['dm_id'] . "'")->select();
            foreach($data['damage'][$key]['urls'] as $k => $v){
                $data['damage'][$key]['urls'][$k]['url'] = get_content_cdnurl().$v['url'];
            }
        }
        //车辆工况信息
        $data['perform'] = M('car_perform')->where("car_id='" . $this->car_id . "'")->find();
        //车主信息
//         $data['owner'] = M('car_owner')->find($data['car_base']['owner_id']);

        $data['owner'] = M('car_owner a')
            ->join(C('DB_PREFIX') . 'come_from b ON a.comefrom=b.from_id')
            ->field('a.*,b.pid')
            ->find($data['car_base']['owner_id']);

        return $data;
    }

    /**
     *
     */
    public static function key_to_carid($keyword) {
        $res = M('cars')->alias('c')->field('car_id')
                ->join(C('DB_PREFIX') . "car_brand cb ON c.brand_id=cb.brand_id")
                ->join(C('DB_PREFIX') . "car_series cs ON c.series_id=cs.series_id")
                ->join(C('DB_PREFIX') . "car_model cm ON c.model_id=cm.model_id")
                ->where("cb.name like '%" . $keyword . "%' OR cs.name like '%" . $keyword . "%' OR cm.name like '%" . $keyword . "%'")
                ->select();
        $car_ids = array();
        foreach ($res as $k => $v) {
            $car_ids[] = $v['car_id'];
        }
        return $car_ids;
    }

    /**
     * 根据线索ID，查询车源信息
     * */
    public static function get_car_by_owner_id($owner_id) {
        return M('cars')->where("owner_id='" . $owner_id . "'")->find();
    }

    /**
     * 根据线索ID，查询车源信息
     * */
    public static function get_car_by_vin($vin) {
        $ret = array();
        $res = M('cars')->where("vin='" . $vin . "' AND (" . time() . "-UNIX_TIMESTAMP(posttime))<=3600*24*4")->order('posttime DESC')->find();
        if ($res) {
            $ret['car_id'] = $res['car_id'];
            $ret['car_no'] = $res['car_no'];
            $ret['owner_id'] = $res['owner_id'];
        }
        return $ret;
    }

    /**
     * 通过车辆编号查询车辆信息
     * @param string $car_no
     * @return boolean|mixed
     */
    public static function get_car_by_car_no($car_no) {
        if (empty($car_no)) {
            return false;
        }
        return M('cars')->where("car_no='" . $car_no . "'")->find();
    }

    public function get_branch_id(){
        $carInfo = M('cars')->where("car_id='". $this->car_id ."'")->find();
        return M('branch_city')->where(['city_code'=>$carInfo['location_area']])->getField("branch_id");
    }

}
